<template>
    <div>
        <h2>修改会员</h2>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>会员编号</td>
                    <td><input type="text" v-model="a.MemberCode" disabled /></td>
                </tr>
                <tr>
                    <td>电话</td>
                    <td><input type="text" v-model="a.Phone" maxlength="11" /></td>
                </tr>
                <tr>
                    <td>名字</td>
                    <td><input type="text" v-model="a.UserName" /></td>
                </tr>
                <tr>
                    <td>身份证</td>
                    <td><input type="text" v-model="a.Card" maxlength="18" /></td>
                </tr>
                <tr>
                    <td>等级</td>
                    <td><select v-model="a.LevelId">
                            <option value="">请选择</option>
                            <option :value="item.Id" v-for="item in data.list">{{ item.LevelName }}</option>
                        </select></td>
                </tr>
                <tr>
                    <td>生日</td>
                    <td><input type="date" v-model="a.Birthday" /></td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td><input type="radio" name="sex" :value="1" v-model="a.Sex" />男
                        <input type="radio" name="sex" :value="0" v-model="a.Sex" />女
                    </td>
                </tr>
                <tr>
                    <td>积分</td>
                    <td><input type="text" v-model="a.Score" /></td>
                </tr>
                <tr>
                    <td>余额</td>
                    <td><input type="text" v-model="a.Money" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="修改" class="btn btn-primary" @click="add" /></td>
                </tr>
            </tbody>
        </table>


    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
import moment from 'moment';

//路由器
const router = useRouter();
//接收参数
const route = useRoute();

//整一个三位随机数100-999
var num = parseInt((Math.random() * 1000).toString()) + 100;

let a = reactive({
    Id: route.query.id,
    MemberCode: moment(new Date()).format('YYYY-MM-DD') + num,
    Phone: '',
    UserName: '',
    Card: '',
    LevelId: '',
    Birthday: '',
    Sex: 1,
    Score: '',
    Money: ''
});

let data: any = reactive({
    list: [],
})
onMounted(() => {

    getList();
    getData();
})
const getList = () => {
    axios({
        method: 'get',
        url: '/api/Member/show'
    })
        .then((res) => {
            console.log(res);
            data.list = res.data;
        })
        .catch((err) => {
            console.log(err);
        });
}
const getData = () => {
    axios({
        method: 'get',
        url: '/api/Member/GetMemberModelItem',
        params: { id: route.query.id }
    })
        .then((res) => {
            console.log(res);


            a.MemberCode = res.data.MemberCode,
                a.Phone = res.data.Phone,
                a.UserName = res.data.UserName,
                a.Card = res.data.Card,
                a.LevelId = res.data.LevelId,
                a.Birthday = res.data.Birthday.substring(0, 10),
                a.Sex = res.data.Sex,
                a.Score = res.data.Score,
                a.Money = res.data.Money
        })
        .catch((err) => {
            console.log(err);
        });
}

const add = () => {
    if (a.Phone == '') {
        alert('手机号不能为空');
        return;
    }
    let reg1 = /^1[3-9]\d{9}$/;
    if (!reg1.test(a.Phone)) {
        alert('手机号不合法');
        return;
    }
    if (a.UserName == '') {
        alert('姓名不能为空');
        return;
    }
    let reg2 = /^\d{17}[0-9xX]$/;
    if (!reg2.test(a.Card)) {
        alert('身份证不合法');
        return;
    }
    if (a.Card == '') {
        alert('身份证号不能为空');
        return;
    }
    if (a.LevelId == '') {
        alert('等级不能为空');
        return;
    }
    if (a.Birthday == '') {
        alert('生日不能为空');
        return;
    }
    if (a.Score == '') {
        alert('积分不能为空');
        return;
    }
    if (isNaN(parseInt(a.Score)) == true) {
        alert("积分必须为数字");
        return;
    }
    if (a.Money == '') {
        alert('余额不能为空');
        return;
    }
    if (isNaN(Number(a.Money))) {
        alert("余额必须为数字");
        return;
    }

    axios({
        method: 'post',
        url: '/api/Member/UpdateMemberItem',
        data: a,
    })
        .then((res) => {
            console.log(res);

            if (res.data == -1) {
                alert("身份证号重复");
                return;
            }
            if (res.data < 0) {
                alert("修改失败");
                return;
            }
            if (res.data > 0) {
                alert("修改成功");
                router.push("/memberlist");
            }
        })
        .catch((err) => {
            console.log(err);
        });
}


</script>

<style scoped></style>